import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import { ElectricSubitemByDay } from "@/api/backend/index";
import { useAssetGateway } from "../Assets/gateway";
import qs from "qs";

export const useElectricSubitemByDay = defineStore(Names.ElectricSubitemByDay, {
  state: () => {
    return {
      type: <any>[],
      ecode: "", //电表号
      siteids: <any>[],
      start: 0,
      end: 23,
      date: "",
      siteid: "",
      filedArr: <any>[], //不同时段组成的不同字段
      cabinetNameArrForTable: new Map([]), //电表对应的机柜名称
      tags: <any>[],
      resetSite: <any>{}, //将机柜类型站点名称与父级站点名称重组
      unit: "时",
    };
  },
  getters: {},
  actions: {
    //获取分析分项列表数据
    getList(filter: any) {
      return new Promise((resolve, reject) => {
        let filterLen = filter.date.split("-").length;
        let url =
          filterLen === 3
            ? ElectricSubitemByDay + "/list?"
            : filterLen === 2
            ? "/system/detailed/SubitemByMonth/list?"
            : "/system/detailed/SubitemByYear/list?";
        _get(
          url + qs.stringify(filter, { arrayFormat: "indices" }),
          {},
          "json",
          ""
        )
          .then((res) => {
            console.log("获取分析分项列表数据", res);

            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //通过网关获取机柜名称
    getCabinet(filter: any) {
      return new Promise((resolve, reject) => {
        const gateway = useAssetGateway();
        gateway
          .getList(filter)
          .then((res: any) => {
            resolve(res);
          })
          .catch((err: any) => {});
      });
    },
  },
});
